class GunDeal {
	public context: MainPanel;
	public gunSigal = 0;
	public needGun: number = -1;  // 需要滚动的次数
	public startMoney: number = 0;  //一开始的金额
	public lastMoney: number = 0; //最近一次金额
	public model = null;  //滚动的检测模式
	public money = 0;
	public constructor(context: MainPanel) {
		this.context = context;
	}
	/**
	* 开始滚动一次
	*/
	public gunRotate(): void {
		if (this.needGun > 0 ){
			this.context.gunPanle.rotationSpeed = 75;
		}else{
			this.context.gunPanle.rotationSpeed = 55;
			this.context.crystalPanle.closeAutoRotation();
			new TeskGun(this);
			this.context.gunPanle.gameSpeedSigal = 1;
			this.gunSigal = 1;
			this.context.moreBtn.moreUI.setAutoSetTouchEvent(false,false);
		}
	}

	private gunMoreRotate()
	{
		this.context.crystalPanle.closeAutoRotation();
		new TeskGun(this);
		this.context.gunPanle.gameSpeedSigal = 1;
		this.gunSigal = 1;
	}

	public reloadData() {
		this.needGun = -1;
		this.model = null;
		this.startMoney = 0;
		this.lastMoney = 0;
		this.gunSigal = 0;
		this.context.crystalPanle.displayUserPanle();
		this.context.moreBtn.moreUI.setAutoSetTouchEvent(true,true);
	}
	public complete(): void {
//		console.log("任务完成");
		var point = this;
		setTimeout(function () {
			nextRotate.call(point);
		}, 500);

		function nextRotate() {
//			console.log("执行下一次滚动");
			if (this.ishasFree()) {
				this.gunMoreRotate();
				return;
			}
			this.needGun--;
			var cuMoney = this.context.leftPanle.getMoney();

			if (isAutoRa.call(this)) {

				this.gunMoreRotate();
				return;
			} else {
				this.reloadData();
			}

			function isAutoRa() {
				if (this.needGun > 0) {
//					console.log("检查滚动条件", this.model);
//					console.log(isAny.call(this));
//					console.log(isOne.call(this));
//					console.log(isPlus.call(this));
//					console.log(isAdd.call(this));


					// if (isAdd.call(this) || isAny.call(this) || isOne.call(this) || isPlus.call(this)) {
					// 	return false;
					// } else {
					// 	return true;

					// }

					if (isHigh.call(this) || isLow.call(this)) {
						return false;
					} else {
						return true;

					}
				} else {
					return false;
				}
			}
			function isAny() {
				if (!this.model) return false;
				if (this.model["any"] && (cuMoney > this.startMoney)) {
					return true;
				} else {
					return false;
				}
			}

			function isOne() {
				if (!this.model) return false;
				if (this.model["one"] && ((cuMoney - this.lastMoney) > this.model["one"])) {
					this.lastMoney = cuMoney;
					return true;
				} else {
					return false;
				}
			}
			function isAdd() {
				if (!this.model) return false;
				if (this.model["add"] && ((cuMoney - this.startMoney) > this.model["add"])) {
					return true;
				} else {
					return false;
				}
			}
			function isPlus() {
				if (!this.model) return false;
				if (this.model["plus"] && (((this.startMoney - cuMoney) > this.model["plus"]))) {
					return true;
				} else {
					return false;
				}
			}
			function isHigh(){
				if (!this.model) return false;
				if(this.model["high"] && (cuMoney > this.model["high"]))
				{
					return true;
				}else {
					return false;
				}
			}

			function isLow(){
				if (!this.model) return false;
				if(this.model["low"] && (cuMoney < this.model["low"]))
				{
					return true;
				}else {
					return false;
				}
			}

		}
	}


    /**
     * 滚动完成后的参数
     */

    /**
     * 设置多次滚动
     */
	public gunSetNumRotate(num: number, model: any): void {
		this.needGun = num;
		this.model = model;
		this.startMoney = this.context.leftPanle.getMoney();
		this.lastMoney = this.context.leftPanle.getMoney();
		this.context.crystalPanle.closeAutoRotation();
		this.context.crystalPanle.autoRotationBtn.visible = true;
		this.context.gunPanle.rotationSpeed = 55;
		this.context.moreBtn.moreUI.setAutoSetTouchEvent(false,true);
		this.gunMoreRotate();
	}
	public gunNumRotate(num: number): void {
		this.needGun = num;
//		console.log("多次滚动的次数", this.needGun);
		this.context.crystalPanle.closeAutoRotation();
		this.context.gunPanle.rotationSpeed = 55;
		this.context.moreBtn.moreUI.setAutoSetTouchEvent(false,true);
		this.gunMoreRotate();
	}

    /**
     * 停止多次转动
     */
	public gunStop(): void {
		//将停止
		this.context.crystalPanle.setStopRotateDisplay();
		if (this.needGun < 1) {
//			console.log("单次停止");
			this.context.gunPanle.gameSpeedSigal = 1;
		} else {
			this.needGun = -1;
			this.model = "";
			this.money = 0;
			this.startMoney = 0;
			this.lastMoney = 0;
			this.context.crystalPanle.isAuto = false;
//			console.log("多次停止");
			// this.context.moreBtn.moreUI.setAutoSetTouchEvent(true,true);
		}

	}
	/**
	 * 判断是否还有免费次数
	 */
	public ishasFree(){
		return  this.context.freePanle.ishasFree();
	}

}